package com.yunfeng.supercall.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.MyCircleBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.ManagerFactory;

/**
 * 创建圈子
 * 
 * @author 洋
 * 
 */
public class CreateCircleActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_circle);
	}

	public void next(View v) {
		String name = getTextViewText(R.id.name);
		String addr = getTextViewText(R.id.addr);
		String jianjie = getTextViewText(R.id.jianjie);
		JSONObject json = getBaseParams(getActivity());
		if (TextUtils.isEmpty(name)) {
			showToast(R.string.input_circle_name);
			return;
		}
		if (TextUtils.isEmpty(addr)) {
			showToast(R.string.input_addr);
			return;
		}
		if (TextUtils.isEmpty(jianjie)) {
			showToast(R.string.input_note);
			return;
		}
		try {
			json.put("i_note", jianjie);
			json.put("i_address", addr);
			json.put("i_name", name);
			json.put("i_photo", "");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		showProgressDialog(getString(R.string.creating));
		mHttpClient.post(FUNCTION_ADD_CIRCLE, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				showToast(msg);
				cancelProgressDialog();
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				showToast(R.string.create_success);
				cancelProgressDialog();
				List<MyCircleBean> circles = JsonUtils.parseList(data,
						MyCircleBean.class);
				List<MyCircleBean> list = (List<MyCircleBean>) ManagerFactory
						.createManager(MyCircleBean.class).getList();
				list.addAll(circles);
				startActivity(new Intent(getActivity(),
						CreateCirlcleAddFriendActivity.class).putExtra(
						KEY_DATA, circles.get(0)));
			}
		});
	}
}
